package com.mfangsoft.zhuangjialong.app.order.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mfangsoft.zhuangjialong.app.order.model.OrderInfo;
import com.mfangsoft.zhuangjialong.app.order.service.IAppOrderService;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.CacheUtil;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;


@Controller
@RequestMapping("/app")
public class AppOrderController {
	
	@Autowired
	private IAppOrderService orderService; 
	
	/**
	 * 新增订单
	 * @param 
	 * @return
	 */
	
	@RequestMapping(value="/addOrder",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> addOrder(@RequestBody OrderInfo order){
		ResponseMessage<String> message = new ResponseMessage<String>();
		try {
			orderService.addOrder(order);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
			
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
		
		
	}
	
	/**
	 * 查询活动详情
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value="/selectOrderDetail",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<OrderInfo> selectOrderDetail(@RequestBody Map<String,Object> map){
		ResponseMessage<OrderInfo> message = new ResponseMessage<OrderInfo>();
		try {
			message.setResult(orderService.selectOrderDetail(Integer.valueOf(map.get("orderId").toString())));
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	/**
	 * 更新活动
	 * @param order
	 * @return
	 */
	@RequestMapping(value="/updateOrder",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> updateOrder(@RequestBody Map<String,Object> order){
		ResponseMessage<String> message = new ResponseMessage<String>();
		try {
			orderService.updateOrder(order);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
		
	}
	/**
	 * 更新活动
	 * @param order
	 * @return
	 */
	@RequestMapping(value="/refreshOrder",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> refreshOrder(@RequestBody Map<String,Object> order){
		ResponseMessage<String> message = new ResponseMessage<String>();
		try {
			orderService.updateRefreshOrder(order);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
		
	}
	
	/**
	 * 导购查看订单列表
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/selectSellerOrderPage",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Page<OrderInfo>> selectSellerOrderPage(@RequestBody Page<OrderInfo> page){
		ResponseMessage<Page<OrderInfo>> message = new ResponseMessage<Page<OrderInfo>>();
		try {
			message.setResult(orderService.selectSellerOrderPage(page));
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	/**
	 * 消费者查看订单列表
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/selectCustomerOrderPage",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Page<OrderInfo>> selectCustomerOrderPage(@RequestBody Page<OrderInfo> page){
		ResponseMessage<Page<OrderInfo>> message = new ResponseMessage<Page<OrderInfo>>();
		try {
			message.setResult(orderService.selectCustomerOrderPage(page));
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	
	/**
	 * 助手关闭订单
	 * @param order
	 * @return
	 */
	@RequestMapping(value="/closeOrder",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> closeOrder(@RequestBody OrderInfo order){
		ResponseMessage<String> message = new ResponseMessage<String>();
		try {
			orderService.updateOrderStatus(order);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			return message;
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
		
	}
	
	@RequestMapping(value="/createOrder",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Map<String,Object>> createOrder(@RequestBody Map<String,Object> map){
		try {
			return orderService.createOrder(map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ResponseMessage<Map<String,Object>> message = new ResponseMessage<Map<String,Object>>();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
	@RequestMapping(value="/checkOrder",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Map<String,Object>> checkOrder(@RequestBody Map<String,Object> map){
		try {
			ResponseMessage<Map<String,Object>> order = orderService.checkOrder(map);
			return order;
			
		} catch (Exception e) {
			e.printStackTrace();
			ResponseMessage<Map<String,Object>> message = new ResponseMessage<Map<String,Object>>();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
		
		
	}
	@RequestMapping(value="/selectCustomerActivity",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<List<Map<String,Object>>> selectCustomerActivity(@RequestBody Map<String,Object> map){
		ResponseMessage<List<Map<String,Object>>> message=new ResponseMessage<List<Map<String,Object>>>();
		try {
			return orderService.selectCustomerActivity(map);
		} catch (Exception e) {
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			return message;
		}
	}
}
